package com.slicktechnologies.shared.common.productlayer;
import com.google.gwt.core.shared.GwtIncompatible;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Index;
import com.itextpdf.text.log.SysoCounter;
import com.simplesoftwares.client.library.appstructure.SuperModel;
import com.simplesoftwares.client.library.helperconstants.FormTypes;
import com.simplesoftwares.rebind.annatonations.TableAnnotation;
import com.slicktechnologies.shared.common.helperlayer.DocumentUpload;

import static  com.googlecode.objectify.ObjectifyService.ofy;

// TODO: Auto-generated Javadoc
/**
 * The Class SuperProduct. Abstracts the common attributes of any product.
 */
@Entity
public class SuperProduct extends SuperModel
{
	
	/** *********************************************Entity Attributes***************************************************. */
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1890L;

	/** The prod code. */
	@Index
	protected String productCode;

	/** The prod name. */
	@Index
	protected String productName;

	/** The spec. */
	protected String specifications;

	/** The comment. */
	protected String comment;

	/** Unit of measurement of the product. */
	protected String unitOfMeasurement;

	/** The price can be (sell/purchase/other) further will be decided as per the business logic */
	protected double price;

	/** The terms and conditions. */
	protected DocumentUpload termsAndConditions;

	/** The lbt tax entity. */
	protected Tax lbtTax;

	/** The status. */
	protected boolean status=true;

	/** The product category. */
	@Index
	protected String productCategory;
	
	/** The product image. */
	protected DocumentUpload productImage;

	/** *********************************************Relational Attributes***************************************************. */
	@Index
	protected Key<Category> categoryKey;

	
	/**
	 * *********************************************Default Ctor***************************************************.
	 */
	/** Instantiates a new super product.*/
	public SuperProduct()
	{
		productImage=new DocumentUpload();
		termsAndConditions=new DocumentUpload();
		lbtTax=new Tax();
		productCode="";
		productName="";
		specifications="";
		comment="";
		unitOfMeasurement="";
		productCategory="";
	}

	/**
	 * *********************************************Getter/Setter***************************************************.
	
	/**
	 * Gets the prod code.
	 *
	 * @return the prod code
	 */
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 2, isFieldUpdater = false, isSortable = true, title = "PCode")
	public String getProductCode() {
		return productCode;
	}

	/**
	 * Sets the prod code.
	 *
	 * @param prodCode the new prod code
	 */
	public void setProductCode(String prodCode)
	{
		if(prodCode!=null)
			this.productCode = prodCode.trim();
	}

	/**
	 * Gets the prod name.
	 *
	 * @return the prod name
	 */
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 3, isFieldUpdater = false, isSortable = true, title = "Name")
	public String getProductName() {
		return productName;
	}

	/**
	 * Sets the prod name.
	 *
	 * @param prodName the new prod name
	 */
	public void setProductName(String prodName) 
	{
		if(prodName!=null)
			this.productName = prodName.trim();
	}

	
	/**
	 * Gets the unit of measurement.
	 *
	 * @return the unit of measurement
	 */
	public String getUnitOfMeasurement() {
		return unitOfMeasurement;
	}

	/**
	 * Sets the unit of measurement.
	 *
	 * @param unitOfMeasurement the new unit of measurement
	 */
	public void setUnitOfMeasurement(String unitOfMeasurement)
	{
		if(unitOfMeasurement!=null)
			this.unitOfMeasurement = unitOfMeasurement.trim();
	}

	/**
	 * Gets the product category.
	 *
	 * @return the product category
	 */
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 1, isFieldUpdater = false, isSortable = true, title = "Category")
	public String getProductCategory() {
		return productCategory;
	}

	/**
	 * Sets the product category.
	 *
	 * @param productCategory the new product category
	 */
	public void setProductCategory(String productCategory) {
		if(productCategory!=null)
			this.productCategory = productCategory.trim();
	}

	/**
	 * Gets the category key.
	 *
	 * @return the category key
	 */
	public Key<Category> getCategoryKey() {
		return categoryKey;
	}

	/**
	 * Sets the category key.
	 *
	 * @param categoryKey the new category key
	 */
	public void setCategoryKey(Key<Category> categoryKey) {
		this.categoryKey = categoryKey;
	}

	/**
	 * Gets the spec.
	 *
	 * @return the spec
	 */
	public String getSpecification() {
		return specifications;
	}

	/**
	 * Sets the spec.
	 *
	 * @param spec the new spec
	 */
	public void setSpec(String spec)
	{
		if(specifications!=null)
			this.specifications = spec.trim();
	}

	/**
	 * Gets the comment.
	 *
	 * @return the comment
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * Sets the comment.
	 *
	 * @param comment the new comment
	 */
	public void setComment(String comment)
	{
		if(comment!=null)
			this.comment = comment.trim();
	}

	/**
	 * Gets the price.
	 *
	 * @return the price
	 */
	@TableAnnotation(ColType = FormTypes.NUMBERTEXTCELL, colNo = 4, isFieldUpdater = false, isSortable = true, title = "Price")
	public Double getPrice() {
		return price;
	}

	/**
	 * Sets the price.
	 *
	 * @param price the new price
	 */
	public void setPrice(double price) {
		this.price = price;
	}

	/**
	 * Gets the term ncondition.
	 *
	 * @return the term ncondition
	 */
	public DocumentUpload getTermNcondition() {
		return termsAndConditions;
	}

	/**
	 * Sets the term ncondition.
	 *
	 * @param termNcondition the new term ncondition
	 */
	public void setTermNcondition(DocumentUpload termNcondition) {
		this.termsAndConditions = termNcondition;
	}


	/**
	 * Gets the lbt tax entity.
	 *
	 * @return the lbt tax entity
	 */
	@TableAnnotation(ColType = FormTypes.TEXTCOLUM, colNo = 5, isFieldUpdater = false, isSortable = false, title = "Lbt")
	public Tax getLbtTax() {
		return lbtTax;
	}

	/**
	 * Sets the lbt tax entity.
	 *
	 * @param lbtTaxEntity the new lbt tax entity
	 */
	public void setLbtTax(Tax lbtTaxEntity) {
		this.lbtTax = lbtTaxEntity;
	}



	/**
	 * Checks if is status.
	 *
	 * @return true, if is status
	 */
	public Boolean isStatus() {
		return status;
	}

	/**
	 * Sets the status.
	 *
	 * @param status the new status
	 */
	public void setStatus(boolean status) {
		this.status = status;
	}

	/**
	 * Gets the specifications.
	 *
	 * @return the specifications
	 */
	public String getSpecifications() {
		return specifications;
	}

	/**
	 * Sets the specifications.
	 *
	 * @param specifications the new specifications
	 */
	public void setSpecifications(String specifications) {
		this.specifications = specifications;
	}

	/**
	 * Gets the terms and conditions.
	 *
	 * @return the terms and conditions
	 */
	public DocumentUpload getTermsAndConditions() {
		return termsAndConditions;
	}

	/**
	 * Sets the terms and conditions.
	 *
	 * @param termsAndConditions the new terms and conditions
	 */
	public void setTermsAndConditions(DocumentUpload termsAndConditions) {
		this.termsAndConditions = termsAndConditions;
	}

	/**
	 * Gets the product image.
	 *
	 * @return the product image
	 */
	public DocumentUpload getProductImage() {
		return productImage;
	}

	/**
	 * Sets the product image.
	 *
	 * @param productImage the new product image
	 */
	public void setProductImage(DocumentUpload productImage) {
		this.productImage = productImage;
	}

	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(SuperModel arg0) {
		SuperProduct per=(SuperProduct)arg0;
		return per.productName.compareTo(productName);
	}


	/* (non-Javadoc)
	 * @see com.simplesoftwares.shared.common.helperlayer.SuperModel#isDuplicate(com.simplesoftwares.shared.common.helperlayer.SuperModel)
	 */
	@Override
	public boolean isDuplicate(SuperModel model) {
		boolean prodNamebool=isDuplicateProductName(model);
		boolean prodCodebool=isDuplicateProductCode(model);
		System.out.println(prodNamebool+"NAME");
		System.out.println(prodCodebool+"CODE");
		System.out.println("Returning"+(prodNamebool&&prodCodebool));
		return prodNamebool||prodCodebool;
		
		
	}
	
	
	public boolean isDuplicateProductCode(SuperModel model) {
		SuperProduct per=(SuperProduct) model;
		String name = per.getProductCode().trim();
		name=name.replaceAll("\\s","");
		name=name.toLowerCase();
		
		String curname=this.productCode.trim();
		curname=curname.replaceAll("\\s","");
		curname=curname.toLowerCase();
		//New Object is being added
		if(per.id==null)
		{
			if(name.equals(curname))
				return true;
			else
				return false;
		}

		// Old object is being updated
		else
		{
			if(per.id.equals(id))
				return false;
			else if (name.equals(curname))
				return true;
			else
				return false;

		}
	}
	

	public boolean isDuplicateProductName(SuperModel model) {
		SuperProduct per=(SuperProduct) model;
		String name = per.getProductName();
		//System.out.println("VALUE OF PRODUCT "+per);
		//System.out.println("VALUE OF PRODUCT NAME "+per.prodName);
		name=name.replaceAll("\\s","");
		name=name.toLowerCase();

		String curname=this.productName.trim();
		curname=curname.replaceAll("\\s","");
		curname=curname.toLowerCase();
		//New Object is being added
		if(per.id==null)
		{
			if(name.equals(curname))
				return true;
			else
				return false;
		}

		// Old object is being updated
		else
		{
			if(per.id.equals(id))
				return false;
			else if (name.equals(curname))
				return true;
			else
				return false;

		}
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return productName;
	}

	@GwtIncompatible
	public void onSave()
	{
		if(productCategory!=null)
		   categoryKey=ofy().load().type(Category.class).filter("catName",this.productCategory).keys().first().now();
		
	}
	
	@GwtIncompatible
	public void onLoad()
	{
	  if(categoryKey!=null)
	  {
		 Category cat=ofy().load().key(categoryKey).now();
		 if(cat!=null)
		 {
			if(cat.getCatName().equals("")==false)
			 this.productCategory=(cat.getCatName());
		 }
		 
	  }
	}
	
}
